#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLineEdit>
#include <QComboBox>
#include <QCheckBox>
#include <QVBoxLayout>
#include <QLabel>
#include <QMessageBox>
#include <QInputDialog>
#include <QString>
#include <QRegularExpression>
#include <QGroupBox>
#include <QCloseEvent>
#include <QThread>
#include "loadconfig.h"
#include "modbusthread.h"
#include "modbus_ctr.h"
#include "csvexporter.h"
#include "graphs.h"
#include "exporter.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

typedef enum
{
    QLINEEDIT,
    QCOMBOX,
    QCHECKBOX,
    NOTHING
}UseWidget;

class RegParam : public QWidget
{
    Q_OBJECT
public:
    RegParam();
    bool ReadOnly        = false;
    bool IsHex           = false;
    bool pause           = false;
    bool IsMultiplier    = false;
    bool AllowAddToGrap  = false;
    bool AllowExport     = false;
    float MultipNum      = 1;
    uint16_t modbusAddr  = 0;
    int16_t val          = 0;                    /* 全部使用有符号16bit保存,c++强转会出问题，后期可使用联合体解决 */
    QString step         = "1";
    UseWidget WidgetType = QLINEEDIT;
    int tabNum = 0;
    int gpNum = 0;
    QWidget *tmp;

    QVector<QMetaObject::Connection> ConnectionList;
    QVector<QMap<int, QString>>      CheckBoxMapList;
    QVector<QCheckBox*>              CheckBoxList;
    QMap<int, QString>               ComBoxMap;
    QLineEdit                        *lineEdit;
    QComboBox                        *comboBox;
    QLabel                           *name;
    QLabel                           *units;
    //获取原始父类，因为控件加入布局后父类会变成布局的地址
    static RegParam* getTestInstance(QLineEdit *lineEdit) {
        return lineEdit->property("testInstance").value<RegParam*>();
//        int
    }
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void SingalsToSlots();
    void UiConstr();
    void CreateBaseWidget();
    void TaskListToExporter();
    void TakeListToGraphs();


    QWidget* CreateTabPage(int num);
    RegParam *CreateWidget(const QStringList &type);
    QMap<int, QString> parseDataToMap(const QString &dataStr);
    QMap<int, QString> parseBitDescriptions(const QString& bitDescriptions);
    QMap<int, RegParam*> findRegParam;
    bool isHexString(const QString &qString, bool allowPrefix = true);

protected:
    bool eventFilter(QObject *obj, QEvent *event)override;
    void closeEvent(QCloseEvent *event) override;

private slots:
    void on_SerialUpdate(QStringList items);
    void on_ModbusDataToUI(QVector<uint16_t> dat);
    void on_ExportTaskDone(const QString &result);
    void on_btnSaveClicked();

private:
    Ui::MainWindow     *ui;
    QLabel             *SerialInfo;
    QComboBox          *SerialComBox;
    QPushButton        *btnConnect;
    QPushButton        *btnSave;
    QPushButton        *btnGraph;
    ModbusThread       *MB_Thread;
    Modbus_ctr         *MB_Ctr;
    LoadConfig         config;
    Graphs             *graphs;
    QThread            *ExportThread;
    Exporter           *exporter;
    QTimer             *OP_Timer;
    QVector<RegParam*> RegParamList;
    QVector<QGridLayout *> layList;

signals:
    void SendSerialConf(LoadConfig *,QString);
    void WriteModbus(uint16_t , uint16_t);

};


#endif // MAINWINDOW_H
